package mybatis.generator;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 代码自动生成工具
 */
public abstract class Generator {

    private static final Logger logger = LoggerFactory.getLogger(Generator.class);
    
    public static void main(String[] args) {
        try {
            List<String> warnings = new ArrayList<String>();
            boolean overwrite = true;
            String configFile = "generator.xml";

            String path = Generator.class.getResource("").getPath();
            // 如果路径路径带中文或空格，需要做解码处理
            path = URLDecoder.decode(path, "UTF-8");

            File inputFile = new File(path + configFile); // new File(Generator.class.getResource(configFile).getFile());
            ConfigurationParser parser = new ConfigurationParser(warnings);
            Configuration config = parser.parseConfiguration(inputFile);

            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            MyBatisGenerator generator = new MyBatisGenerator(config, callback, warnings);
            generator.generate(null);
        }
        catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
}
